package com.cms.service;

import java.util.List;

import com.cms.model.AppInfo;
import com.cms.model.AppScreenshot;
import com.cms.model.AppVersionInfo;
import com.cms.model.AppversionTerraceInfo;
import com.cms.model.TerraceInfo;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class AppInfoServiceImpl extends AbstractDateService implements
		AppInfoService {
	
	@Override
	public boolean deleteAppInfo(AppInfo model) {
		String sql = "update t_appinfo set isdel='1' where appnumber='"+model.getAppnumber()+"'";
		logger.info("[SQL] [method:deleteAppInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public AppInfo getAppInfoByAppNumber(String appNumber) {
		String sql = "select * from t_appinfo where appnumber='"+appNumber+"' order by createdate desc";
		logger.info("[SQL] [method:getAppInfoByAppNumber] ["+sql+"]");
		ModelGenerator<AppInfo> model = new ModelGenerator<AppInfo>(AppInfo.class);
		return model.generate(base,sql.toString());
	}

	@Override
	public List<AppInfo> getAppInfos() {
		String sql = "select * from t_appinfo where isdel='0'  order by createdate desc";
		logger.info("[SQL] [method:getAppInfos] ["+sql+"]");
		ModelGenerator<AppInfo> model = new ModelGenerator<AppInfo>(AppInfo.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public List<AppInfo> queryAppInfo(int cpid, int categoryparentid,
			int categoryid, String appName) {
		String sql = "select * from t_appinfo  where 1=1 and isdel='0' ";
		if(cpid>0)
		{
			sql+=" and cpid='"+cpid+"' ";
		}
		if(categoryparentid>0)
		{
			sql+=" and categoryparentid='"+categoryparentid+"' ";
		}
		if(categoryid>0)
		{
			sql+=" and categoryid='"+categoryid+"' ";
		}
		if(appName!=null && appName.length()>0)
		{
			sql+=" and appname like '%"+appName+"%' ";
		}
		sql+=" order by createdate desc";
		logger.info("[SQL] [method:queryAppInfo] ["+sql+"]");
		ModelGenerator<AppInfo> model = new ModelGenerator<AppInfo>(AppInfo.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public boolean saveAppInfo(AppInfo model) {
		String sql = "insert into t_appinfo (appnumber,appname,cpid,categoryparentid,categoryid"+
		",apppackage,appstatus,appversion,appmd5,appiconname,reiconname,appiconsrc,appfilename"+
		",refilename,appfilesrc,appentry,createuser,createdate,appremark) " +
		"values ('"+model.getAppnumber()+"','"+model.getAppname()+"','"+model.getCpid()+"','"+model.getCategoryparentid()+"','"+model.getCategoryid()+
		"','"+model.getApppackage()+"','"+model.getAppstatus()+"','"+model.getAppversion()+"','"+model.getAppmd5()+"','"+model.getAppiconname()+
		"','"+model.getReiconname()+"','"+model.getAppiconsrc()+"','"+model.getAppfilename()+"','"+model.getRefilename()+"','"+model.getAppfilesrc()+
		"','"+model.getAppentry()+"','"+model.getCreateuser()+"','"+model.getCreatedate()+"','"+model.getAppremark()+"')";
		logger.info("[SQL] [method:saveAppInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateAppInfo(AppInfo model) {
		String sql = "update t_appinfo set appname='" + model.getAppname() + "'," +
					"apppackage='" + model.getApppackage() + "', " +
					"appversion='" + model.getAppversion() + "' " +
					",appmd5='" + model.getAppmd5()+ "' " +
					",cpid='" + model.getCpid()+ "' " +
					",appiconname='" + model.getAppiconname() + "' " +
					",reiconname='" + model.getReiconname() + "' " +
					",appiconsrc='" + model.getAppiconsrc() + "' " +
					",appfilename='" + model.getAppfilename() + "' " +
					",refilename='" + model.getRefilename() + "' " +
					",appfilesrc='" + model.getAppfilesrc() + "' " +
					",appentry='" + model.getAppentry() + "' " +
					",appremark='" + model.getAppremark() + "' " +
					",categoryparentid='" + model.getCategoryparentid() + "' " +
					",categoryid='" + model.getCategoryid() + "' " +
					" where appnumber='" + model.getAppnumber() + "'";
		logger.info("[SQL] [method:updateAppInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateAppFile(AppInfo model) {
		String sql = "update t_appinfo set appfilename='" + model.getAppfilename() + "' " +
			",refilename='" + model.getRefilename() + "' " +
			",appfilesrc='" + model.getAppfilesrc() + "' " +
			" where appnumber='" + model.getAppnumber() + "'";
		logger.info("[SQL] [method:updateAppFile] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
}

	@Override
	public boolean deleteAppScreenshot(AppScreenshot model) {
		String sql = "delete from  t_appscreenshot where id='"+model.getId()+"'";
		logger.info("[SQL] [method:deleteAppScreenshot] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public List<AppScreenshot> getAppScreenshotByAppNumber(String appNumber) {
		String sql = "select * from t_appscreenshot where appnumber='"+appNumber+"'  order by id desc";
		logger.info("[SQL] [method:getAppScreenshotByAppNumber] ["+sql+"]");
		ModelGenerator<AppScreenshot> model = new ModelGenerator<AppScreenshot>(AppScreenshot.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public List<AppScreenshot> getAppScreenshotByAppVersionNumber(
			String appversionNumber,String type) {
		String sql = "select * from t_appscreenshot where appversionnumber='"+appversionNumber+"'";
		if(type!=null&&!"".equals(type)){
			sql+=" and type='"+type+"'";
		}
		sql+="  order by id desc";
		logger.info("[SQL] [method:getAppScreenshotByAppVersionNumber] ["+sql+"]");
		ModelGenerator<AppScreenshot> model = new ModelGenerator<AppScreenshot>(AppScreenshot.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public List<AppScreenshot> getAppScreenshotBychannelapkNumber(
			String channelapkNumber,String type) {
		String sql = "select * from t_appscreenshot where channelappnumber='"+channelapkNumber+"'";
		if(type!=null&&!"".equals(type)){
			sql+=" and type='"+type+"'";
		}
		sql+=" order by id desc";
		logger.info("[SQL] [method:getAppScreenshotBychannelapkNumber] ["+sql+"]");
		ModelGenerator<AppScreenshot> model = new ModelGenerator<AppScreenshot>(AppScreenshot.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public List<AppScreenshot> getAppScreenshots() {
		String sql = "select * from t_appscreenshot  order by id desc";
		logger.info("[SQL] [method:getAppScreenshots] ["+sql+"]");
		ModelGenerator<AppScreenshot> model = new ModelGenerator<AppScreenshot>(AppScreenshot.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public AppScreenshot getAppScreenshotById(String id) {
		String sql = "select * from t_appscreenshot  where id="+id;
		logger.info("[SQL] [method:getAppScreenshotById] ["+sql+"]");
		ModelGenerator<AppScreenshot> model = new ModelGenerator<AppScreenshot>(AppScreenshot.class);
		return model.generate(base,sql.toString());
	}

	@Override
	public List<AppScreenshot> getAppScreenshotByModel(AppScreenshot model) {
		String sql = "select * from t_appscreenshot where 1=1 ";
		if(null!=model.getAppnumber()&&!"".equals(model.getAppnumber())){
			sql+="and appnumber='"+model.getAppnumber()+"' ";
		}
		if(null!=model.getAppversionnumber()&&!"".equals(model.getAppversionnumber())){
			sql+="and appversionnumber='"+model.getAppversionnumber()+"' ";
		}
		if(null!=model.getChannelappnumber()&&!"".equals(model.getChannelappnumber())){
			sql+="and channelappnumber='"+model.getChannelappnumber()+"' ";
		}
		if(null!=model.getType()&&!"".equals(model.getType())){
			sql+=" and type='"+model.getType()+"' ";
		}
		if(null!=model.getGenre()&&!"".equals(model.getGenre())){
			sql+=" and genre='"+model.getGenre()+"' ";
		}
		sql+=" order by id desc";
		logger.info("[SQL] [method:getAppScreenshotByModel] ["+sql+"]");
		ModelGenerator<AppScreenshot> m = new ModelGenerator<AppScreenshot>(AppScreenshot.class);
		return m.generateList(base,sql.toString());
	}

	@Override
	public boolean saveAppScreenshot(AppScreenshot model) {
		model.setId(getTableId("t_appscreenshot"));
		String sql = "insert into t_appscreenshot (id,appnumber,imgname,imgrename,imgsrc,type,genre,appversionnumber,channelappnumber) " +
		"values ('"+model.getId()+"','"+model.getAppnumber()+"','"+model.getImgname()+"','"+model.getImgrename()+"','"+model.getImgsrc()+"','"+model.getType()+"','"+model.getGenre()+"','"+model.getAppversionnumber()+"','"+model.getChannelappnumber()+"')";
		logger.info("[SQL] [method:saveAppScreenshot] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean saveAppInfoStatus(AppInfo model) {
		String sql = "update t_appinfo set appstatus='" + model.getAppstatus()+ "' " +
				" where appnumber='" + model.getAppnumber() + "'";
		logger.info("[SQL] [method:updateAppInfo] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public List<AppversionTerraceInfo> queryTerraceInfo() {
		String sql="select distinct tt.id,tt.operatorsName, tas.appnumber,tat.relevanceid,tat.remark as glremark from t_appversionterrace tat,  t_appversioninfo tas,  t_terrace tt "+
				"where  tat.terraceId = tt.id and tat.appversionId = tas.id "+
				"group by tt.id ,tas.appnumber "+
				"order by tt.id ,tas.appnumber ";
		logger.info("[SQL] [method:queryTerraceInfo] ["+sql+"]");
		ModelGenerator<AppversionTerraceInfo> dbcpmg = new ModelGenerator<AppversionTerraceInfo>(AppversionTerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}
	/**
	 * 查看应用下的版本
	 */
	@Override
	public List<AppVersionInfo> queryAppVersionInfo() {
		String sql = "select  tas.appnumber,tas.provinceids ,tas.appname,tas.appversion from  `t_appversioninfo` AS `tas`, `t_appinfo` AS `ta` "				
				+" where `ta`.`appnumber` = `tas`.`appnumber`";
			logger.debug("sql: " + sql);
		ModelGenerator<AppVersionInfo> dbcpmg = new ModelGenerator<AppVersionInfo>(AppVersionInfo.class);
		return dbcpmg.generateList(base, sql);
	}
	
}
